package com.szzg10.thinking.chapter18;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.concurrent.TimeUnit;

public class FileLocking {

	public static void main(String[] args) throws IOException, InterruptedException {
		File file = new File(BasicFileOutput.FILE_NAME_OUT);
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		FileChannel channel = fileOutputStream.getChannel();
		
		FileLock lock = channel.tryLock();
		if(lock != null) {
			System.out.println("Locked File");
			TimeUnit.MILLISECONDS.sleep(100);
			
			lock.release();
			System.out.println("Released File");
		}
		
		channel.close();
		fileOutputStream.close();
	}
}
